package v1

import (
	dblayer "easycharts/db"
	"easycharts/utils/errmsg"
	"fmt"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func GetDemoList(c *gin.Context) {
	data, code := dblayer.GetDemoList()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

func GetTagList(c *gin.Context) {
	data, code := dblayer.GetTagList()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

////////////////////////////////////////////////////

// 查询员工基本信息
func QueryWorker(c *gin.Context) {
	// 解析请求参数
	// 获取员工id和车队id
	id, _ := strconv.Atoi(c.Query("id"))
	name := c.Query("teamid")
	fmt.Println("Param: ", c.Param("name"), " ", c.Param("id"))

	fmt.Println(name, " ", id)

	data, code := dblayer.QueryWorker(id, name)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 获取线路列表
func GetRouteList(c *gin.Context) {
	data, code := dblayer.GetRouteList()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 获取公司列表
func GetCompanyList(c *gin.Context) {
	data, code := dblayer.GetCompanyList()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 获取所有车队
func GetTeamList(c *gin.Context) {
	data, code := dblayer.GetTeamList()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 获取违章类型信息
func GetVioclass(c *gin.Context) {
	data, code := dblayer.GetVioclass()
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 根据司机id查询司机信息
func QueryDriverInfo(c *gin.Context) {
	// 获取司机id
	id := c.Query("id")
	data, code := dblayer.QueryDriverInfo(id)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询某个车队下的司机基本信息
func QueryTeamDrivers(c *gin.Context) {
	// 解析请求参数
	// 获取车队id
	teamId := c.Query("tid")

	data, code := dblayer.QueryTeamDrivers(teamId)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询某名司机在某个时间段的违章详细信息
func QueryDriverViolation(c *gin.Context) {
	// 解析请求参数
	// 获取司机id，和所指定的时段
	driverId := c.Query("id")
	start := c.Query("starttime")
	end := c.Query("endtime")

	fmt.Println("QueryDriverViolation: id= ", driverId,"from", start,"to", end)
	data, code := dblayer.QueryDriverViolation(driverId, start, end)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// 查询某个车队在某个时间段的违章统计信息，如：2次闯红灯、4次未礼让斑马线等
func QueryTeamViolation(c *gin.Context) {
	// 解析请求参数
	// 获取车队id，和所指定的时段
	teamId := c.Query("tid")
	start := c.Query("starttime")
	end := c.Query("endtime")

	fmt.Println("QueryTeamViolation: tid=", teamId,"from", start,"to", end)
	data, code := dblayer.QueryTeamViolation(teamId, start, end)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

